/**
  @file: DomainHost.java

   PURPOSE:
      This class represents an instance of a HostDef in a Domain, either logical
      or physical.

   @author Ron Williams

   REVISION HISTORY ABSTRACT

   PROGRAMMER:  Ron Williams ORG: MSE

   Copyright 2011 Mission Solutions LLC. All rights reserved.

   END ABSTRACT
 */

package com.mse.utilities;

import java.lang.String;
import java.lang.StringBuilder;

import com.mse.utilities.DomainDefLimits;
import com.mse.utilities.DomainException;
import com.mse.utilities.DomainHostArchDef;

/**
   DESCRIPTION:
      The DomainHost class contains the information as a result of parsing a
      Domain Definition file for a Host definition. The attributes for the Host
      are user defined.

      In the future, a discovery process could generate this information.
 */
public class DomainHost {

	/**
      DESCRIPTION:
         Create a DomainHost object.
      @return void
      @throws n/a
	 */
	public DomainHost() {
		this.hid      = "";
		this.hostName = "";
		this.archDef  = null;
		this.isInHostGroup = false;
	}

	/**
      DESCRIPTION:
         Create a DomainHost object based on specified attributes. The hostName
         must NOT be blank if this host is part of a "physical" domain! 

      @param hid      - string containing the Host's reference identifier
      @param hostName - string identifying the Host's network name (for a 
                        physical domain), otherwise it should be blank
      @param archDef  - the Host's architecture identifier
      @return void
      @throws DomainException - for invalid arguments
	 */
	public DomainHost(String hid, String hostName, DomainHostArchDef archDef) 
	throws DomainException {
		// verify input string sizes are not too long
		if (hid.length() > DomainDefLimits.HD_HID_Max) {
			throw new DomainException("HostDef, HID field value is too " +
					"large, can not exceed length: " +
					DomainDefLimits.HD_HID_Max);
		}
		if (hostName.length() > DomainDefLimits.HD_HostName_Max) {
			throw new DomainException("HostDef, hostName field value is too " +
					"large, can not exceed length: " +
					DomainDefLimits.HD_HostName_Max);
		}

		this.hid      = hid;
		this.hostName = hostName;
		this.archDef  = archDef;
		this.isInHostGroup = false;
	}


	/**
      DESCRIPTION:
         get the host reference id.
      @return hid - this host's reference ID
      @throws n/a
	 */
	public String getHID() {
		return hid;
	}

	/**
	   DESCRIPTION:
	      get the hostName. (empty if this host is part of logical domain)
	   @return hostName - this host's OS type identifier
	   @throws n/a
		 */
		public String getHostName() {
			return hostName;
		}

	/**
	   DESCRIPTION:
	      get the host's architecture definition
	   @return archDef - this host's architecture definition
	   @throws n/a
		 */
		public DomainHostArchDef getArchDef() {
			return archDef;
		}

	/**
	   DESCRIPTION
	   	 tells if host is in host group
	   @return isInHostGroup - whether or not it is in host group
	   @throws n/a 
		 */
		public boolean getIsInHostGroup(){
			return isInHostGroup;
		}

	/**
	   DESCRIPTION:
	      set the host reference id.
	   @param refId - what to set the hid to
	   @return n/a
	   @throws n/a
		 */
		public void setHID(String refId) {
			hid = refId;
			return;
		}

	/**
      DESCRIPTION:
         set the hostName. (empty if this host is part of logical domain)
      @return hostName - this host's OS type identifier
      @throws n/a
	 */
	public void setHostName(String hn) {
		hostName = hn;
		return;
	}

	/**
      DESCRIPTION:
         sets the host's architecture definition
      @param dhad - this host's architecture definition to set to 
      @return n/a
      @throws n/a
	 */
	public void setArchDef(DomainHostArchDef dhad) {
		archDef=dhad;
		return;
	}

	/**
      DESCRIPTION
      	 sets IsInHostGroup variable
      @param bool - new value for isInHostGroup
      @return n/a
      @throws n/a 
	 */
	public void setIsInHostGroup(boolean bool){
		isInHostGroup = bool;
		return;
	}

	/**
      DESCRIPTION:
         compare two DomainHost objects for equality. We consider them
         equal if either of the following attributes are the same.
            - hid
            - hostName  (not compared if blank)

      @param other - the DomainHost to be compaired to this one
      @return boolean - true means objects are considered the same, 
                        false otherwise
      @throws n/a
	 */
	public boolean equals(DomainHost other) {
		boolean rslt = false;      // assume they are not equal

		if (this.hid.equals(other.getHID()) ||
				(! this.hostName.equals("") &&
						this.hostName.equals(other.getHostName()) )) {
			rslt = true;
		}

		return rslt;
	}

	/**
      DESCRIPTION:
         obtain the string representation for this host
      @return text - this host's info in a string for output
      @throws n/a
	 */
	public String toString() {
		StringBuilder sb = new StringBuilder(128);

		sb.append("Domain Host - HID: ");
		sb.append(hid);
		sb.append("\n     hostName: ");
		sb.append(hostName);
		sb.append("\n     hostArch: ");
		sb.append(archDef.toString());
		return sb.toString();
	}


	//---------------------------------------------------------------PRIVATE
	private String hid;          // Host Reference Identifier
	private String hostName;     // the network host name, when part of a
	// physical domain definition. Empty string
	// when part of logical domain
	private DomainHostArchDef archDef; // the HostArchDef for this host
	private boolean isInHostGroup;
}

